require(['jquery', 'echarts', 'dataTool'], function($, echarts){
    var net;
    var network1 = $('#network1');
    myChart = echarts.init(network1.get(0));
    myChart.showLoading();
    //原始网络
    $.get('../static/data/complex/complex-network.json', function (graph) {
        net = graph;
        myChart.hideLoading();
        for(var i = 0;i < graph.nodes.length;i++){
            graph.nodes[i].name = graph.nodes[i].name.toString();
        }
        for(var j = 0;j < graph.edges.length;j++){
            graph.edges[j].source = graph.edges[j].source.toString();
            graph.edges[j].target = graph.edges[j].target.toString();
        }
        option = {
            tooltip: {},
            series : [{
                type: 'graph',
                layout: 'force',
                symbolSize: 5,
                roam: true,
                force : { //力引导图基本配置
                        //initLayout: ,//力引导的初始化布局，默认使用xy轴的标点
                        repulsion : 100,//节点之间的斥力因子。支持数组表达斥力范围，值越大斥力越大。
                        gravity : 0.03,//节点受到的向中心的引力因子。该值越大节点越往中心点靠拢。
                        edgeLength :80,//边的两个节点之间的距离，这个距离也会受 repulsion。[10, 50] 。值越小则长度越长
                        layoutAnimation : false
                    //因为力引导布局会在多次迭代后才会稳定，这个参数决定是否显示布局的迭代动画，在浏览器端节点数据较多（>100）的时候不建议关闭，布局过程会造成浏览器假死。
                },
                edgeSymbol: ['circle', 'arrow'],
                data: graph.nodes,
                links: graph.edges,
                label: {
                    normal: {
                        show: false,
                        textStyle: {
                            fontSize: 10
                        },
                    }
                },
                edgeLabel: {
                    show:true,
                    normal: {
                        textStyle: {
                            fontSize: 20
                        }
                    }
                },
                normal: {
                    width: 0.3,
                    curveness: 0.3,
                    opacity: 0.7
                }
            }]
        };
        myChart.setOption(option);

    });

    //节点中心性指标计算
    $("#nodeAlgo").on('click', function (e) {
        var id = e.target.id;
        var dataArray = [];
        var algoArray = ["algo1", "algo2", "algo3", "algo4", "algo5", "algo6", "algo7", "algo8", "algo9"];
        var api = -1;
        for (var i = 0; i < algoArray.length; i++) {
            if (id === algoArray[i]) {
                api = i;
                for (var j = 0; j < net.edges.length; j++) {
                    dataArray[j] = new Array(4);
                    for (var k = 0; k < 2; k++) {
                        if (k === 0) {
                            dataArray[j][k] = parseInt(net.edges[j].source)
                        }
                        dataArray[j][k] = parseInt(net.edges[j].target)
                    }
                    dataArray[j][2] = dataArray[j][3] = 1;
                }
                break;
            }
        }
        api = api + 2;
        $.ajax({
            type: "POST",
            url: "http://bdilab.tju.edu.cn/networkapi/",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({
                data: dataArray,
                api: api
            }),
            dataType: "JSON",
            success: function (data, message) {
                var cnt = 20;
                var network2 = $('#network2');
                myChart1 = echarts.init(network2.get(0));
                myChart1.clear();
                myChart1.showLoading();
                myChart1.hideLoading();
                if (api === 5) {
                    $.get('../static/data/complex/complex-network.json', function (graph){
                        for (var i = 0; i < graph.nodes.length; i++) {
                            graph.nodes[i].name = graph.nodes[i].name.toString();
                        }
                        for (var j = 0; j < graph.edges.length; j++) {
                            graph.edges[j].source = graph.edges[j].source.toString();
                            graph.edges[j].target = graph.edges[j].target.toString();
                        }
                        graph.edges.forEach(link => {
                            link.lineStyle = {
                                color: (cnt--) > 0 ? 'blue' : 'grey'
                            };
                            Object.keys(data).forEach(function (v, k) {
                                if((link.source == data[v][0]) && (link.target == data[v][1])){
                                    var wid = data[v][2] * 10 + 3;
                                    link.lineStyle = {
                                        width: wid,
                                        color: (cnt--) > 0 ? 'blue' : 'grey'
                                    }
                                }
                            })
                        });
                        option = {
                            tooltip: {},
                            series: [{
                                type: 'graph',
                                layout: 'force',
                                roam: true,
                                force: {
                                    repulsion: 150,
                                    edgeLength: 200,
                                    layoutAnimation : false
                                },
                                edgeSymbol: ['', 'arrow'],
                                data: graph.nodes,
                                links: graph.edges,
                                label: {
                                    normal: {
                                        show: false,
                                        textStyle: {
                                            fontSize: 10
                                        },
                                    }
                                },
                                edgeLabel: {
                                    show: true,
                                    normal: {
                                        textStyle: {
                                            fontSize: 20
                                        }
                                    }
                                },
                                normal: {
                                    width: 0.3,
                                    curveness: 0.3,
                                    opacity: 0.7
                                }
                            }]
                        };
                        myChart1.setOption(option);
                    })
                }
                else {
                    $.get('../static/data/complex/complex-network.json', function (graph) {
                        for (var i = 0; i < graph.nodes.length; i++) {
                            graph.nodes[i].name = graph.nodes[i].name.toString();
                        }
                        for (var j = 0; j < graph.edges.length; j++) {
                            graph.edges[j].source = graph.edges[j].source.toString();
                            graph.edges[j].target = graph.edges[j].target.toString();
                        }
                        graph.nodes.forEach(node =>{
                            if(!isNaN(data[node.name])){
                                node.symbolSize = Math.abs(data[node.name] * 100) + 10;
                            }
                        });
                        option = {
                            tooltip: {},
                            series: [{
                                type: 'graph',
                                layout: 'force',
                                symbolSize:5,
                                roam: true,
                                force: {
                                    repulsion: 150,
                                    edgeLength: 200,
                                    layoutAnimation : false
                                },
                                edgeSymbol: ['', 'arrow'],
                                data: graph.nodes,
                                links: graph.edges,
                                label: {
                                    normal: {
                                        show: false,
                                        textStyle: {
                                            fontSize: 10
                                        },
                                    }
                                },
                                edgeLabel: {
                                    show: true,
                                    normal: {
                                        textStyle: {
                                            fontSize: 20
                                        }
                                    }
                                },

                            }]
                        };
                        myChart1.setOption(option);
                    });
                }
                if (message > 0) {
                    alert("请求已提交！我们会尽快与您取得联系");
                }
            },
            error: function (message) {
                $("#request-process-patent").html("提交数据失败！");
            }
        });
    });

    //链接预测
    $("#linkAlgo").on('click', function (e) {
        var id = e.target.id;
        var dataArray = [];
        var algoArray = ["algo10", "algo11", "algo12", "algo13"];
        var api = -1;
        for (var i = 0; i < algoArray.length; i++) {
            if (id === algoArray[i]) {
                api = i;
                for (var j = 0; j < net.edges.length; j++) {
                    dataArray[j] = new Array(4);
                    for (var k = 0; k < 2; k++) {
                        if (k === 0) {
                            dataArray[j][k] = parseInt(net.edges[j].source)
                        }
                        dataArray[j][k] = parseInt(net.edges[j].target)
                    }
                    dataArray[j][2] = dataArray[j][3] = 1;
                }
                break;
            }
        }
        api = api + 11;
        $.ajax({
            type: "POST",
            url: "http://bdilab.tju.edu.cn/networkapi/",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({
                data: dataArray,
                api: api
            }),
            dataType: "JSON",
            success: function (data, message) {
                var cnt = 15;
                data=data.slice(0,cnt);
                var network2 = $('#network2');
                myChart2 = echarts.init(network2.get(0));
                myChart2.clear();
                myChart2.showLoading();
                myChart2.hideLoading();
                $.get('../static/data/complex/complex-network.json', function (graph) {
                    for (var i = 0; i < graph.nodes.length; i++) {
                        graph.nodes[i].name = graph.nodes[i].name.toString();
                    }
                    for (var j = 0; j < graph.edges.length; j++) {
                        graph.edges[j].source = graph.edges[j].source.toString();
                        graph.edges[j].target = graph.edges[j].target.toString();
                    }
                    graph.edges.forEach(link => {
                        Object.keys(data).forEach(function (v, k) {
                            if ((link.source == data[v][0]) && (link.target == data[v][1])) {
                                var wid = data[v][2] > 1?data[v][2]:(data[v][2] * 10 + 3);
                                link.lineStyle = {
                                    width: wid,
                                    color:'blue'
                                }
                            }
                        })
                    });
                    option = {
                        tooltip: {},
                        series: [{
                            type: 'graph',
                            layout: 'force',
                            roam: true,
                            force: {
                                repulsion: 150,
                                edgeLength: 200,
                                layoutAnimation: false
                            },
                            edgeSymbol: ['', 'arrow'],
                            data: graph.nodes,
                            links: graph.edges,
                            label: {
                                normal: {
                                    show: false,
                                    textStyle: {
                                        fontSize: 10
                                    },
                                }
                            },
                            edgeLabel: {
                                show: true,
                                normal: {
                                    textStyle: {
                                        fontSize: 20
                                    }
                                }
                            },
                            normal: {
                                width: 0.3,
                                curveness: 0.3,
                                opacity: 0.7
                            }
                        }]
                    };
                    myChart2.setOption(option);
                });
                if (message > 0) {
                    alert("请求已提交！我们会尽快与您取得联系");
                }
            },
            error: function (message) {
                $("#request-process-patent").html("提交数据失败！");
            }
        });
    });

    //社团检测
    $("#communityAlgo").on('click', function (e) {
        var id = e.target.id;
        var dataArray = [];
        var algoArray = ["algo14", "algo15", "algo16"];
        var api = -1;
        for (var i = 0; i < algoArray.length; i++) {
            if (id === algoArray[i]) {
                api = i;
                for (var j = 0; j < net.edges.length; j++) {
                    dataArray[j] = new Array(4);
                    for (var k = 0; k < 2; k++) {
                        if (k === 0) {
                            dataArray[j][k] = parseInt(net.edges[j].source)
                        }
                        dataArray[j][k] = parseInt(net.edges[j].target)
                    }
                    dataArray[j][2] = dataArray[j][3] = 1;
                }
                break;
            }
        }
        api = api + 15;
        var postData = (api === 15) ? JSON.stringify({
            data: dataArray,
            para: 2,
            api: api
        }) : JSON.stringify({data: dataArray, api: api});
        $.ajax({
            type: "POST",
            url: "http://bdilab.tju.edu.cn/networkapi/",
            contentType: "application/json; charset=utf-8",
            data: postData,
            dataType: "JSON",
            success: function (data, message) {
                var network2 = $('#network2');
                var cnt = 0;
                var categories = [];
                myChart3 = echarts.init(network2.get(0));
                myChart3.clear();
                myChart3.showLoading();
                myChart3.hideLoading();
                $.get('../static/data/complex/complex-network.json', function (graph) {
                    for (var i = 0; i < graph.nodes.length; i++) {
                        graph.nodes[i].name = graph.nodes[i].name.toString();
                    }
                    for (var j = 0; j < graph.edges.length; j++) {
                        graph.edges[j].source = graph.edges[j].source.toString();
                        graph.edges[j].target = graph.edges[j].target.toString();
                    }
                    for (let [key, value] of Object.entries(data)) {
                        var community = Array.from(value);
                        community.forEach(function (value) {
                            graph.nodes.forEach(node => {
                                if(value == node.name){
                                    node.category = ++cnt;
                                    console.log(node);
                                }
                            })
                        })
                    }
                    for (var i = 0; i < cnt; i++) {
                        categories[i] = {
                            name: '社团' + (i + 1)
                        };
                    }
                    option = {
                        tooltip: {},
                        legend: [{
                            data: categories.map(function (a) {
                                return a.name;
                            })
                        }],
                        series: [{
                            type: 'graph',
                            layout: 'force',
                            roam: true,
                            force: {
                                repulsion: 150,
                                edgeLength: 200,
                                layoutAnimation: false
                            },
                            edgeSymbol: ['', 'arrow'],
                            categories:categories,
                            data: graph.nodes,
                            links: graph.edges,
                            label: {
                                normal: {
                                    show: false,
                                    textStyle: {
                                        fontSize: 10
                                    },
                                }
                            },
                            edgeLabel: {
                                show: true,
                                normal: {
                                    textStyle: {
                                        fontSize: 20
                                    }
                                }
                            },
                            normal: {
                                width: 0.3,
                                curveness: 0.3,
                                opacity: 0.7
                            }
                        }]
                    };
                    myChart3.setOption(option);
                });
                if (message > 0) {
                    alert("请求已提交！我们会尽快与您取得联系");
                }
            },
            error: function (message) {
                $("#request-process-patent").html("提交数据失败！");
            }
        });
    });

});